<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <title>系统设置</title>
    <link rel="shortcut icon" type="image/x-icon" th:href="@{/static/img/favicon.ico}" media="screen"/>
    <link rel="stylesheet" th:href="@{/static/css/layui.css}" media="all"/>
</head>
<style>
    .layui-form-pane .layui-form-label {
        width: 120px;
    }
    .btnClass .layui-input-inline {
        width: 165px !important;
    }
</style>
<body>

<div style="margin:5px;">
    <form class="layui-form layui-form-pane" action="" lay-filter="val-filter">
        <div class="layui-bg-gray" style="padding: 16px;">
            <div class="layui-row layui-col-space15">
                <div class="layui-col-md4">
                    <div class="layui-card">
                        <div class="layui-card-header" style="background-color: #16baaa">参数配置</div>
                        <div class="layui-card-body">
                            <div class="layui-form-item" style="padding-top: 10px;">
                                <label class="layui-form-label" pane>系统版本</label>
                                <div class="layui-input-inline">
                                    <input type="text" name="" placeholder="当前版本" class="layui-input"
                                           th:value="${version.localVersion}"
                                           disabled>
                                </div>
                                <div class="layui-form-mid layui-text-em" th:if="${version.push == true}">
                                    <span class="layui-badge-dot"></span>
                                    <a th:href="${version.link}" target="_blank">最新版本已发布,请及时更新!</a>
                                </div>
                            </div>
                            <div class="layui-form-item">
                                <label class="layui-form-label" pane>数据加载</label>
                                <div class="layui-input-inline">
                                    <select name="initDataSource" id="initDataSource">
                                        <option value="1">开启</option>
                                        <option value="2">关闭</option>
                                    </select>
                                    <input type="hidden" name="id" th:value="${obj.id}">
                                    <input type="hidden" id="initDataSourceVal" th:value="${obj.initDataSource}">
                                </div>
                                <div class="layui-form-mid layui-text-em" lay-on="tips-right1">
                                    <i class="layui-icon layui-icon-tips"></i>
                                </div>
                            </div>

                            <div class="layui-form-item">
                                <label class="layui-form-label" pane>日志记录</label>
                                <div class="layui-input-inline">
                                    <select name="enabledSqlLog" id="enabledSqlLog">
                                        <option value="1">开启</option>
                                        <option value="2">关闭</option>
                                    </select>
                                    <input type="hidden" id="enabledSqlLogVal" th:value="${obj.enabledSqlLog}">
                                </div>
                                <div class="layui-form-mid layui-text-em" lay-on="tips-right2">
                                    <i class="layui-icon layui-icon-tips"></i>
                                </div>
                            </div>

                            <div class="layui-form-item">
                                <label class="layui-form-label" pane>锁屏展示</label>
                                <div class="layui-input-inline">
                                    <select name="enabledLockView" id="enabledLockView">
                                        <option value="0">开启</option>
                                        <option value="1">关闭</option>
                                    </select>
                                    <input type="hidden" id="enabledLockViewVal" th:value="${obj.enabledLockView}">
                                </div>
                                <div class="layui-form-mid layui-text-em" lay-on="tips-right3">
                                    <i class="layui-icon layui-icon-tips"></i>
                                </div>
                            </div>

                            <div class="layui-form-item">
                                <label class="layui-form-label" pane>文档展示</label>
                                <div class="layui-input-inline">
                                    <select name="enabledHelp" id="enabledHelp">
                                        <option value="0">开启</option>
                                        <option value="1">关闭</option>
                                    </select>
                                    <input type="hidden" id="enabledHelpVal" th:value="${obj.enabledHelp}">
                                </div>
                                <div class="layui-form-mid layui-text-em" lay-on="tips-right4">
                                    <i class="layui-icon layui-icon-tips"></i>
                                </div>
                            </div>

                            <div class="layui-form-item">
                                <label class="layui-form-label" pane>提示信息</label>
                                <div class="layui-input-inline">
                                    <select name="enabledHint" id="enabledHint">
                                        <option value="0">开启</option>
                                        <option value="1">关闭</option>
                                    </select>
                                    <input type="hidden" id="enabledHintVal" th:value="${obj.enabledHint}">
                                </div>
                                <div class="layui-form-mid layui-text-em" lay-on="tips-right8">
                                    <i class="layui-icon layui-icon-tips"></i>
                                </div>
                            </div>

                            <div class="layui-form-item">
                                <label class="layui-form-label" pane>windows通知</label>
                                <div class="layui-input-inline">
                                    <select name="enabledNotification" id="enabledNotification">
                                        <option value="0">开启</option>
                                        <option value="1">关闭</option>
                                    </select>
                                    <input type="hidden" id="enabledNotificationVal"
                                           th:value="${obj.enabledNotification}">
                                </div>
                                <div class="layui-form-mid layui-text-em" lay-on="tips-right9">
                                    <i class="layui-icon layui-icon-tips"></i>
                                </div>
                            </div>


                            <div class="layui-form-item">
                                <label class="layui-form-label" pane>分页条数</label>
                                <div class="layui-input-inline" >
                                    <input type="text" id="pageLimitMax" name="pageLimitMax"
                                           th:value="${obj.pageLimitMax}"
                                           lay-affix="number"
                                           class="layui-input">
                                </div>
                                <div class="layui-form-mid layui-text-em" lay-on="tips-right5">
                                    <i class="layui-icon layui-icon-tips"></i>
                                </div>
                            </div>

                            <div class="layui-form-item">
                                <label class="layui-form-label" pane>语句限制</label>
                                <div class="layui-input-inline" >
                                    <input type="text" id="riskText" name="riskText" th:value="${obj.riskText}"
                                           class="layui-input"
                                           lay-affix="clear">
                                </div>
                                <div class="layui-form-mid layui-text-em" lay-on="tips-right6">
                                    <i class="layui-icon layui-icon-tips"></i>
                                </div>
                            </div>

                            <div class="layui-form-item">
                                <label class="layui-form-label" pane>失败次数</label>
                                <div class="layui-input-inline" >
                                    <input type="text" id="failLogin" name="failLogin" th:value="${obj.failLogin}"
                                           lay-affix="number"
                                           class="layui-input">
                                </div>
                                <div class="layui-form-mid layui-text-em" lay-on="tips-right7">
                                    <i class="layui-icon layui-icon-tips"></i>
                                </div>
                            </div>

                            <div class="layui-form-item">
                                <div class="layui-input-block">
                                    <button class="layui-btn layui-btn-primary layui-border-green" lay-submit
                                            lay-filter="save">
                                        <i class="layui-icon layui-icon-add-1"> </i>保存设置
                                    </button>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="layui-col-md8">
                    <div class="layui-card">
                        <div class="layui-card-header" style="background-color: #16baaa">维护操作</div>
                        <div class="layui-card-body">
                            <div class="layui-form-item btnClass">
                                <div class="layui-input-inline">
                                    <button class="layui-btn layui-btn-primary layui-border-red layui-btn-radius layui-btn-sm"
                                            onclick="btnPost('清除SQL执行日志','logManager/deleteLog/1');return false;">
                                        <i class="layui-icon layui-icon-clear"> </i>清空SQL执行记录
                                    </button>
                                </div>
                                <div class="layui-input-inline">
                                    <button class="layui-btn layui-btn-primary layui-border-red layui-btn-radius layui-btn-sm"
                                            onclick="btnPost('清除用户登陆日志','logManager/deleteLog/2');return false;">
                                        <i class="layui-icon layui-icon-clear"> </i>清空登陆系统记录
                                    </button>
                                </div>
                                <div class="layui-input-inline">
                                    <button class="layui-btn layui-btn-primary layui-border-red layui-btn-radius layui-btn-sm"
                                            onclick="btnPost('清除作业日志','timingManager/jobLogDelete');return false;">
                                        <i class="layui-icon layui-icon-clear"> </i>清空作业执行记录
                                    </button>
                                </div>
                                <div class="layui-input-inline">
                                    <button class="layui-btn layui-btn-primary layui-border-red layui-btn-radius layui-btn-sm"
                                            onclick="btnPost('清除SQL常用记录','sqlManager/sqlTextDeleteAll');return false;">
                                        <i class="layui-icon layui-icon-clear"> </i>清空SQL常用列表
                                    </button>
                                </div>
                                <div class="layui-input-inline">
                                    <button class="layui-btn layui-btn-primary layui-border-red layui-btn-radius layui-btn-sm"
                                            onclick="btnPost('清空数据源列表','settingManager/deleteDataSourceAll');return false;">
                                        <i class="layui-icon layui-icon-clear"> </i>清空数据源列表
                                    </button>
                                </div>
                                <div class="layui-input-inline">
                                    <button class="layui-btn layui-btn-primary layui-border-red layui-btn-radius layui-btn-sm"
                                            onclick="btnPost('清空系统缓存','settingManager/clearCache');return false;">
                                        <i class="layui-icon layui-icon-clear"> </i>清空系统缓存
                                    </button>
                                </div>
                            </div>

                            <div class="layui-form-item btnClass">
                                <div class="layui-input-inline">
                                    <button class="layui-btn layui-btn-primary layui-border-blue layui-btn-radius layui-btn-sm"
                                            onclick="downloadFile('settingManager/downloadTeamJson');return false;">
                                        <i class="layui-icon layui-icon-download-circle"> </i>导出团队信息
                                    </button>
                                </div>

                                <div class="layui-input-inline">
                                    <button class="layui-btn layui-btn-primary layui-border-blue layui-btn-radius layui-btn-sm"
                                            onclick="downloadFile('settingManager/downloadDataSourceJson');return false;">
                                        <i class="layui-icon layui-icon-download-circle"> </i>导出数据源信息
                                    </button>
                                </div>

                                <div class="layui-input-inline">
                                    <button class="layui-btn layui-btn-primary layui-border-blue layui-btn-radius layui-btn-sm"
                                            onclick="downloadFile('settingManager/downloadSqlTextJson');return false;">
                                        <i class="layui-icon layui-icon-download-circle"> </i>导出SQL文本信息
                                    </button>
                                </div>

                                <div class="layui-input-inline">
                                    <button class="layui-btn layui-btn-primary layui-border-blue layui-btn-radius layui-btn-sm"
                                            onclick="btnPost('重建数据源连接池','settingManager/reloadDataSourceAll');return false;">
                                        <i class="layui-icon layui-icon-refresh"> </i>重建数据源连接池
                                    </button>
                                </div>

                                <div class="layui-input-inline">
                                    <button class="layui-btn layui-btn-primary layui-border-red layui-btn-radius layui-btn-sm"
                                            onclick="btnPost('清空监测日志记录','detectionManager/deleteLogAll');return false;">
                                        <i class="layui-icon layui-icon-clear"> </i>清空监测日志记录
                                    </button>
                                </div>

                            </div>

                            <div class="layui-form-item btnClass">
                                <div class="layui-input-inline">
                                    <button type="button" class="layui-btn layui-btn-radius layui-btn-sm" id="upload-teamData" lay-on="tips-right11"
                                            lay-options="{accept: 'file'}">
                                        <i class="layui-icon layui-icon-upload"></i>
                                        导入团队信息1
                                    </button>
                                </div>

                                <div class="layui-input-inline">
                                    <button type="button" class="layui-btn layui-btn-radius layui-btn-sm upload-datasource" id="upload-datasource"
                                            lay-on="tips-right10"
                                            lay-options="{accept: 'file'}">
                                        <i class="layui-icon layui-icon-upload"></i>
                                        导入数据源信息2
                                    </button>
                                </div>

                                <div class="layui-input-inline">
                                    <button type="button" class="layui-btn layui-btn-radius layui-btn-sm" id="upload-sqlText" lay-on="tips-right11"
                                            lay-options="{accept: 'file'}">
                                        <i class="layui-icon layui-icon-upload"></i>
                                        导入SQL文本信息3
                                    </button>
                                </div>
                            </div>
                        </div>
                    </div>
                    <div class="layui-card">
                        <div class="layui-card-header" style="background-color: #16baaa">告警模板配置</div>
                        <table class="layui-hide" id="dataTable" lay-filter="dataTable"></table>
                    </div>
                </div>
            </div>
        </div>
    </form>
</div>
<script th:src="@{/static/layui.js}"></script>
<script th:replace="~{base::ctx}"/>
<script type="text/html" id="toolbarTable">
    <div class="layui-clear-space">
        <a class="layui-btn layui-btn-xs" lay-event="view">内容</a>
        <a class="layui-btn layui-bg-red layui-btn-xs" lay-event="delete">删除</a>
    </div>
</script>
<script type="text/html" id="addBtnHtml">
    <div class="layui-btn-container">
        <button class="layui-btn layui-btn-sm layui-btn-primary layui-border-yellow" onclick="openPage('新增告警配置',ctx+'settingManager/addMessageTemplatePage');return false;">新增</button>
        <button class="layui-btn layui-btn-sm  layui-btn-primary layui-border-blue" id="search"  lay-filter="table-search">刷新</button>
    </div>
</script>
<script>

    layui.use(function () {
        const layer = layui.layer;
        const form = layui.form;
        const $ = layui.jquery;
        const util = layui.util;
        const upload = layui.upload;
        const table = layui.table;
        const laytpl = layui.laytpl;

        $("#initDataSource").each(function () {
            $(this).children("option").each(function () {
                if (this.value === $("#initDataSourceVal").val()) {
                    $(this).attr("selected", "selected");
                }
            });
        });
        $("#enabledSqlLog").each(function () {
            $(this).children("option").each(function () {
                if (this.value === $("#enabledSqlLogVal").val()) {
                    $(this).attr("selected", "selected");
                }
            });
        });

        $("#enabledLockView").each(function () {
            $(this).children("option").each(function () {
                if (this.value === $("#enabledLockViewVal").val()) {
                    $(this).attr("selected", "selected");
                }
            });
        });

        $("#enabledHelp").each(function () {
            $(this).children("option").each(function () {
                if (this.value === $("#enabledHelpVal").val()) {
                    $(this).attr("selected", "selected");
                }
            });
        });

        $("#enabledHint").each(function () {
            $(this).children("option").each(function () {
                if (this.value === $("#enabledHintVal").val()) {
                    $(this).attr("selected", "selected");
                }
            });
        });

        $("#enabledNotification").each(function () {
            $(this).children("option").each(function () {
                if (this.value === $("#enabledNotificationVal").val()) {
                    $(this).attr("selected", "selected");
                }
            });
        });


        form.render();

        //上传数据源初始化
        upload.render({
            elem: '#upload-datasource',
            url: ctx + 'settingManager/uploadDataSourceJson',
            exts: 'json',
            dataType: 'json',
            done: function (res) {
                layer.alert(res.msg, {icon: res.code === 200 ? 1 : 2});
            }, error: function (index, upload) {
                layer.msg("上传失败,请重试!", {icon: 2});
                console.log(index, upload);
            }
        });

        //上传sql文本信息初始化
        upload.render({
            elem: '#upload-sqlText',
            url: ctx + 'settingManager/uploadSqlTextJson',
            exts: 'json',
            dataType: 'json',
            done: function (res) {
                layer.alert(res.msg, {icon: res.code === 200 ? 1 : 2});
            }, error: function (index, upload) {
                layer.msg("上传失败,请重试!", {icon: 2});
                console.log(index, upload);
            }
        });

        //上传sql文本信息初始化
        upload.render({
            elem: '#upload-teamData',
            url: ctx + 'settingManager/uploadTeamJson',
            exts: 'json',
            dataType: 'json',
            done: function (res) {
                layer.alert(res.msg, {icon: res.code === 200 ? 1 : 2});
            }, error: function (index, upload) {
                layer.msg("上传失败,请重试!", {icon: 2});
                console.log(index, upload);
            }
        });

        // 提示信息
        const tipsConfig = {tips: [2, '#16b777']};
        util.on('lay-on', {
            'tips-right1': function () {
                layer.tips('开启时，启动项目时会主动加载数据源，启动速度慢，受链接失败影响。<br>关闭时，项目启动不会加载数据源，需要手动加载数据源，启动速度快，不会受链接失败影响。', this, tipsConfig);
            },
            'tips-right2': function () {
                layer.tips('开启时，执行SQL会记录SQL执行日志及返回结果，时间长会增大数据库文件;<br>关闭时,不记录SQL执行日志，会缺失操作记录找不到操作人。', this, tipsConfig);
            },
            'tips-right3': function () {
                layer.tips('开启时会显示锁屏按钮，且需要输入密码重新进入;<br>关闭时会隐藏锁屏按钮', this, tipsConfig);
            },
            'tips-right4': function () {
                layer.tips('开启时会显示帮助按钮;<br>关闭时会隐藏帮助按钮', this, tipsConfig);
            },
            'tips-right5': function () {
                layer.tips('默认查询时分页最大查询条数<br>SQL执行时没有分页条件会走此限制,sql中有分页条件不会走此限制<br>填0不开启此限制。<br>支持mysql、oracle、H2、postgres、达梦、人大金仓、神州通用', this, tipsConfig);
            },
            'tips-right6': function () {
                layer.tips('执行SQL时检查语句包含危险关键词不允许执行;<br>小写输入关键词支持多个用,分割', this, tipsConfig);
            },
            'tips-right7': function () {
                layer.tips('账号登录失败次数,超过次数状态将被禁用!<br>普通账号使用超管账号修改用户状态<br>填写‘-1’不开启禁用账号，这样会很危险!<br>admin账号重置密码 <a target="_blank" href="https://gitee.com/boy_0214/websql/wikis/%E5%B8%B8%E8%A7%81%E9%97%AE%E9%A2%98">gitee Wiki</a>', this, tipsConfig);
            },
            'tips-right8': function () {
                layer.tips('编辑器写SQL时会提示选中的数据源所有表及字段!<br>如果数据库中超过上万字段不建议开启会卡死!<br>支持mysql、oracle、H2、postgres、达梦、人大金仓、神州通用', this, tipsConfig);
            },
            'tips-right9': function () {
                layer.tips('执行SQL完成后,页面离开态会进行Windows系统通知!<br>关闭将不会在发送通知，但不影响执行SQL使用。', this, tipsConfig);
            },
            'tips-right10': function () {
                layer.tips('导入数据源时,确保地址正确能连接，不会覆盖原有数据!<br>如果导入的团队不存在，会绑定右上角选中的团队信息中。<br> 导入多条数据出现1条失败其余的都会成功,下次只需要导入失败条数。', this, tipsConfig);
            },
            'tips-right11': function () {
                layer.tips('导入sql文本信息,不会覆盖原有数据!<br>如果导入的团队不存在，会绑定右上角选中的团队信息中。<br> 导入多条数据出现1条失败其余的都会成功,下次只需要导入失败条数。', this, tipsConfig);
            }
        })

        form.on('submit(save)', function (data) {
            const field = data.field;
            window.localStorage.removeItem('site');
            post(ctx + "settingManager/updateSysSetUp", field, (res) => {
                layer.msg(res.msg, {icon: res.code === 200 ? 1 : 2});
            });
            return false;
        });

        table.render({
            elem: '#dataTable',
            url: ctx + 'settingManager/messageTemplateList',
            defaultToolbar: ['filter', 'exports', 'print'],
            page: true,
            height: '500',
            toolbar: '#addBtnHtml',
            cols: [
                [
                    {field: 'id', title: 'id', sort: true, fixed: true},
                    {field: 'name', title: '名称', sort: true, fixed: true},
                    {field: 'type', title: '类型', sort: true, fixed: true},
                    {field: 'url', title: '地址', sort: true, fixed: true},
                    {field: 'secret', title: '签名', sort: true, fixed: true},
                    {field: 'content', title: '内容', sort: true, fixed: true, width: 200},
                    {field: 'createUser', title: '创建人', sort: true, fixed: true},
                    {field: 'createTime', title: '创建时间',width: 200, sort: true, fixed: true},
                    {fixed: 'right', title: '操作', width: 100, toolbar: '#toolbarTable'}
                ]
            ],
            parseData: res => parseData(res)
        });
        table.on('tool(dataTable)', function (obj) {
            var data = obj.data;
            switch (obj.event) {
                case 'view':
                    const content = laytpl('<div style="margin: 5px;"><pre class="layui-code">{{= d.content}}</pre></div>')
                        .render({content: data.content});
                    openContent("查看内容", content);
                    break;
                case 'delete':
                    layer.confirm("您确认要删除此任务吗？", function () {
                        post(ctx + "settingManager/deleteMessageTemplate?id=" + data.id, null, (res) => {
                            layer.msg(res.msg, {icon: res.code === 200 ? 1 : 2}, () => {
                                table.reloadData('dataTable', {
                                    scrollPos: true
                                });
                            });
                        })
                    })
                    break;
            }
        });

        form.on('submit(table-search)', function (data) {
            const field = data.field;
            table.reloadData('dataTable', {
                scrollPos: true,
                where: field
            });
            return false;
        });

    });


    /**
     * 按钮请求后台操作
     * @param msg
     * @param url
     */
    function btnPost(msg, url) {
        layer.confirm("确认" + msg + "？", function () {
            post(ctx + url, null, (res) => {
                layer.msg(res.msg, {icon: res.code === 200 ? 1 : 2});
            });
        });
    }

    /**
     * 下载请求
     * @param url
     */
    function downloadFile(url) {
        layer.confirm("导出的数据没有加密,确保数据安全？", function () {
            window.open(ctx + url);
            layer.closeAll();
        });
    }

</script>
</body>
</html>